package com.gingko.duominuo.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gingko.duominuo.R
import com.gingko.duominuo.data.model.WorkOrderVo
import com.gingko.duominuo.databinding.ItemWorkOrderRecordBinding
import com.gingko.duominuo.utils.NameMappingUtils
import com.gingko.duominuo.utils.millis2String
import com.gingko.duominuo.utils.string2Millis

class WordOrderRecordAdapter(val context: Context, val data: List<WorkOrderVo>) : RecyclerView.Adapter<WordOrderRecordAdapter.WordOrderRecordHolder>() {

    private val defaultTimeFormat = "yyyy/MM/dd"


    inner class WordOrderRecordHolder(private val viewBinding: ItemWorkOrderRecordBinding) : RecyclerView.ViewHolder(viewBinding.root) {

        @SuppressLint("SetTextI18n")
        fun bind(workOrderVo: WorkOrderVo, position: Int) {
//            proj：项目名称
//            WordOrderRecord:任务类型
//            app:平台
//            WordOrderRecord_count:可领取的任务总数量
//            job_id:获取订单号和审核文案的id
            viewBinding.tvTaskProject.text = NameMappingUtils.getProjectName(workOrderVo.proj) ?: "--"
            viewBinding.tvTaskType.text = NameMappingUtils.getTaskName(workOrderVo.task) ?: "--"
            viewBinding.tvTaskPlatform.text = NameMappingUtils.getPlatformName(workOrderVo.app) ?: "--"
//0订单未上报 1 完成 -2 失效
            when (workOrderVo.status) {
                0 -> {
                    viewBinding.tvSubmitStatus.text = "进行中"
                    viewBinding.tvSubmitStatus.setTextColor(ContextCompat.getColor(context, R.color.textPrimary))
                }
                1 -> {
                    viewBinding.tvSubmitStatus.text = "提交成功"
                    viewBinding.tvSubmitStatus.setTextColor(ContextCompat.getColor(context, R.color.textLink))
                }
                -2 -> {
                    viewBinding.tvSubmitStatus.text = "超时作废"
                    viewBinding.tvSubmitStatus.setTextColor(ContextCompat.getColor(context, R.color.color_f45859))
                }
                else -> viewBinding.tvSubmitStatus.text = ""
            }
            viewBinding.tvSubmitDate.text = formatTime(workOrderVo.created_at)
        }

        private fun formatTime(time: String?): String {
            return time?.let {
                try {
                    string2Millis(it, "yyyy-MM-dd HH:mm:ss").millis2String(defaultTimeFormat)
                } catch (e: Exception) {
                    "--"
                }
            } ?: "--"
        }

    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordOrderRecordHolder {
        return WordOrderRecordHolder(ItemWorkOrderRecordBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: WordOrderRecordHolder, position: Int) {
        holder.bind(data[position], position)
    }

    override fun getItemCount(): Int {
        return data.size
    }
}